package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNWarningSwBo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningSwVo;
import com.ruoyi.zhgd.domain.ZjNWarningSw;
import com.ruoyi.zhgd.mapper.ZjNWarningSwMapper;
import com.ruoyi.zhgd.service.IZjNWarningSwService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水稳拌合站预警信息Service业务层处理
 *
 * @author tll
 * @date 2024-09-27
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNWarningSwServiceImpl implements IZjNWarningSwService {

    private final ZjNWarningSwMapper baseMapper;

    /**
     * 查询水稳拌合站预警信息
     */
    @Override
    public ZjNWarningSwVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站预警信息列表
     */
    @Override
    public TableDataInfo<ZjNWarningSwVo> queryPageList(ZjNWarningSwBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjNWarningSw> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(ZjNWarningSw::getInsterTime);
        Page<ZjNWarningSwVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站预警信息列表
     */
    @Override
    public List<ZjNWarningSwVo> queryList(ZjNWarningSwBo bo) {
        LambdaQueryWrapper<ZjNWarningSw> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNWarningSw> buildQueryWrapper(ZjNWarningSwBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNWarningSw> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjNWarningSw::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), ZjNWarningSw::getGrade, bo.getGrade());
        lqw.eq(bo.getInsterTime() != null, ZjNWarningSw::getInsterTime, bo.getInsterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessId()), ZjNWarningSw::getBusinessId, bo.getBusinessId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjNWarningSw::getStatus, bo.getStatus());
        lqw.eq(bo.getDisposalStatus() != null, ZjNWarningSw::getDisposalStatus, bo.getDisposalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), ZjNWarningSw::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptCode()), ZjNWarningSw::getDeptCode, bo.getDeptCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBuzTime()), ZjNWarningSw::getBuzTime, bo.getBuzTime());
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), ZjNWarningSw::getMatchName, bo.getMatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getWarnItem()), ZjNWarningSw::getWarnItem, bo.getWarnItem());
        lqw.eq(StringUtils.isNotBlank(bo.getOutRange()), ZjNWarningSw::getOutRange, bo.getOutRange());
        lqw.eq(StringUtils.isNotBlank(bo.getCz()), ZjNWarningSw::getCz, bo.getCz());
        if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
            lqw.ge(ZjNWarningSw::getInsterTime, bo.getStartTime());
            lqw.le(ZjNWarningSw::getInsterTime, bo.getEndTime());
        }
        return lqw;
    }

    /**
     * 新增水稳拌合站预警信息
     */
    @Override
    public Boolean insertByBo(ZjNWarningSwBo bo) {
        ZjNWarningSw add = BeanUtil.toBean(bo, ZjNWarningSw.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站预警信息
     */
    @Override
    public Boolean updateByBo(ZjNWarningSwBo bo) {
        ZjNWarningSw update = BeanUtil.toBean(bo, ZjNWarningSw.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNWarningSw entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站预警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
